

#include "animation.h"
#include "draw.h"
  #include "init.h"
   #include "input.h"
    #include "main.h"
      #include "map.h"
         #include "player.h"


  void initializePlayer(void)
   {

       player.sprite = loadImage("graphics/walkright.png");

    player.direction = RIGHT;
    player.etat = IDLE;


    player.frameNumber = 0;
    player.frameTimer = TIME_BETWEEN_2_FRAMES;


    player.x = 0;
    player.y = 0;


    player.w = PLAYER_WIDTH;
    player.h = PLAYER_HEIGTH;


    player.timerMort = 0;
    player.onGround = 0;


   }

       void drawplayer()
{

    SDL_Rect dest;

    dest.x = player.x - map.startX;
    dest.y = player.y - map.startY;
    dest.w = PLAYER_WIDTH;
    dest.h = PLAYER_HEIGTH;


    SDL_Rect src;

    src.x = player.frameNumber * PLAYER_WIDTH;
    src.y = 0;
    src.w = PLAYER_WIDTH;
    src.h = PLAYER_HEIGTH;



    SDL_BlitSurface(player.sprite, &src, jeu.screen, &dest);

}
  void updatePlayer(void)
 {
int a=10;
  
if (player.timerMort == 0)
  {

    player.dirX = 0;

    player.dirY += GRAVITY_SPEED;

    if (player.dirY >= MAX_FALL_SPEED)
    {
        player.dirY = MAX_FALL_SPEED;
    }

a=arduino();    
     if (a == 0)
    {
        player.dirX -= PLAYER_SPEED;
        player.direction = LEFT;

        if(player.etat != WALK_LEFT && player.onGround == 1)
        {
            player.etat = WALK_LEFT;
            player.sprite = loadImage("graphics/walkleft.png");
            player.frameNumber = 0;
        }
    }

    else if (input.right == 1)
    {
        player.dirX += PLAYER_SPEED;
        player.direction = RIGHT;

        if(player.etat != WALK_RIGHT && player.onGround == 1)
        {
            player.etat = WALK_RIGHT;
            player.sprite = loadImage("graphics/walkright.png");
            player.frameNumber = 0;
        }
    }

    else if(input.right == 0 && a== 1 && player.onGround == 1)
    {

        if(player.etat != IDLE)
        {
            player.etat = IDLE;

            if(player.direction == LEFT)
            {
                player.sprite = loadImage("graphics/IdleLeft.png");
                player.frameNumber = 0;
            }
            else
            {
                player.sprite = loadImage("graphics/IdleRight.png");
                player.frameNumber = 0;
            }

        }

    }

    if (input.jump == 1 && player.onGround)
    {
        player.dirY = -JUMP_HEIGHT;
        player.onGround = 0;

    }
    if(player.onGround == 0)
    {
        if(player.direction == RIGHT && player.etat != JUMP_RIGHT)
        {
            player.etat = JUMP_RIGHT;
            player.sprite = loadImage("graphics/JumpRight.png");
            player.frameNumber = 0;
        }
        else if(player.direction == LEFT && player.etat != JUMP_LEFT)
        {
            player.etat = JUMP_LEFT;
            player.sprite = loadImage("graphics/JumpLeft.png");
            player.frameNumber = 0;
        }

    }

    mapCollision(&player);
    centerScrollingOnPlayer();

  }

    if (player.timerMort > 0)
    {
        player.timerMort--;

        if (player.timerMort == 0)
        {
            initializePlayer();
        }
    }

  }

 void centerScrollingOnPlayer(void)
 {

    map.startX = player.x - (SCREEN_WIDTH / 2);

    if (map.startX < 0)
    {
        map.startX = 0;
    }

    else if (map.startX + SCREEN_WIDTH >= map.maxX)
    {
        map.startX = map.maxX - SCREEN_WIDTH;
    }

    map.startY = player.y - (SCREEN_HEIGHT / 2);

    if (map.startY < 0)
    {
        map.startY = 0;
    }

    else if (map.startY + SCREEN_HEIGHT >= map.maxY)
    {
        map.startY = map.maxY - SCREEN_HEIGHT;
    }
 }

int arduino()
{
FILE *f=NULL;
int a;

f=fopen("///dev/ttyACM0","r");

while(!feof(f))
{
fscanf(f,"%d",&a);


}
printf("%d",a);
fclose(f);
return a;
}

